package demo.java.a_tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

//继承标签处理器类
public class demo01_showIP extends SimpleTagSupport {
	//传入pageContext
	/*private JspContext context;
	@Override
	public void setJspContext(JspContext pc) {
		this.context=pc;
	}*/
	//覆盖doTag方法
	@Override
	public void doTag() throws JspException, IOException {
		//原始版
		//1、得到PageContext对象
		//PageContext pageContext=(PageContext)context;
		//2、得到HttpServletRequest对象
		//HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		//简化版
		PageContext pageContext=(PageContext)this.getJspContext();
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		//3、得到客户IP地址
		String ip=request.getRemoteHost();
		//4、输出到浏览器
		//4.1、得到JspWriter对象
		JspWriter out=pageContext.getOut();
		//4.2、输出
		out.write(ip);
	}
}
